package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.ImgRecordRequest;
import com.ytjj.qmyx.admin.model.request.ImgTaskUploadRequest;
import com.ytjj.qmyx.admin.service.ImgTaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;

/**
 * @Description 美工接单管理
 * @ClassName ImgTaskController
 *
 *
 * @Version 1.0
 **/

@RestController
@RequestMapping("/imgTask")
public class ImgTaskController {

    @Resource
    private ImgTaskService imgTaskService;

    /**
     * @Description 图片上传
     *
     *
     *
     */
    @PostMapping("/imgUpload")
    @OperLog(operModule = "美工接单模块", operDesc = "图片上传", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult imgUpload(@RequestBody ImgTaskUploadRequest imgTaskUploadRequest) {
        int count = imgTaskService.addImg(imgTaskUploadRequest);
        if(count > 0) {
            return CommonResult.success(ResultCode.SUCCESS);
        }
        return CommonResult.success(ResultCode.FAILED);
    }

    /**
     * @Description 获取图片列表
     *
     *
     *
     * @Version 1.0
     **/
    @PostMapping("/getImgList")
    public CommonResult getImgList(@RequestBody ImgTaskUploadRequest imgTaskUploadRequest) {
        return CommonResult.success(imgTaskService.getImgList(imgTaskUploadRequest));
    }

   /**
    * @Description 删除图片
    *
    *
    *
    * @Version 1.0
    **/
    @GetMapping("/deleteImg")
    @OperLog(operModule = "美工接单模块", operDesc = "删除图片", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult deleteImg(@RequestParam("id") Integer id) {
        int count = imgTaskService.deleteImg(id);
        if(count > 0) {
            return CommonResult.success(ResultCode.SUCCESS);
        }
        return CommonResult.success(ResultCode.FAILED);
    }

    /**
     * @Description 修改图片信息
     *
     *
     *
     */
    @PostMapping("/modifiedImgInfo")
    @OperLog(operModule = "美工接单模块", operDesc = "修改图片信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult modifiedImgInfo(@RequestBody ImgTaskUploadRequest imgTaskUploadRequest) {
        int count = imgTaskService.modifiedImgInfo(imgTaskUploadRequest);
        if(count > 0) {
            return CommonResult.success(ResultCode.SUCCESS);
        }
        return CommonResult.success(ResultCode.FAILED);
    }



    /**
     * 规格列表
     */
    @GetMapping("/getSelectProductSkuInfo")
    public CommonResult getSelectProductSkuInfo(@RequestParam("id") Integer id) {
        return CommonResult.success(imgTaskService.getSelectProductSkuInfo(id));
    }


    /**
     * 根据商品id查询选择的sku属性key
     * @param id
     * @return
     */
    @GetMapping(value = "/selectSkuKeysbyPid")
    public CommonResult selectSkuKeysbyId(@RequestParam Integer id) {
        Map<String,Object> datas = imgTaskService.selectSkuKeysbypid(id);
        return CommonResult.success(datas);
    }

    /**
     * 获取图片操作记录
     *
     * @param imgRecordRequest
     * @return
     */
    @PostMapping("/getImgRecordList")
    public CommonResult getImgRecordList(@RequestBody ImgRecordRequest imgRecordRequest) {
        return CommonResult.success(Optional.ofNullable(imgTaskService.getImgRecordList(imgRecordRequest))
                                     .orElseGet(() -> CommonPage.restPage(new ArrayList<>())));
    }
}
